package com.jboltai.capability.embedding;

import com.jboltai.config.JBoltAIConfig;
import com.jboltai.event.embedding.EmbeddingEvent;
import com.jboltai.resource.embedding.EmbeddingModel;
import com.jboltai.resource.embedding.EmbeddingResource;
import com.jboltai.resource.embedding.EmbeddingResourceCenter;
import com.jboltai.scheduler.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 文本向量化
 */
public class JBoltEmbedding {

    private static final Logger LOGGER = LoggerFactory.getLogger(JBoltAIConfig.LOGGER_NAME);


    /**
     * 注册Embedding模型资源，返回一个资源对象，对象id随机生成
     * @param model
     * @param apiKey
     * @return
     */
    public static EmbeddingResource registerResource(EmbeddingModel model, String apiKey) {
        EmbeddingResource resource = new EmbeddingResource(model, apiKey);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }


    /**
     * 注册一个ai模型资源，返回一个资源对象，对象id使用指定的id
     * @param model
     * @param id
     * @param apiKey
     * @return
     */
    public static EmbeddingResource registerResource(EmbeddingModel model, String id, String apiKey) {
        EmbeddingResource resource = new EmbeddingResource(model, id, apiKey);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }
    /**
     * 注册一个ai模型资源，返回一个资源对象，对象id使用指定的id
     * @param model
     * @param id
     * @param apiKey
     * @return
     */
    public static EmbeddingResource registerResource(EmbeddingModel model, String id, String apiKey, String secretKey) {
        EmbeddingResource resource = new EmbeddingResource(model, id, apiKey, secretKey);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }

    public static EmbeddingResource registerResource(EmbeddingModel model,String id,String appId, String apiKey, String appSecret) {
        EmbeddingResource resource = new EmbeddingResource(model, id, appId,apiKey, appSecret);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }

    /**
     * 注册本地Embedding模型资源
     * @param model
     * @param apiUrl
     * @return
     */
    public static EmbeddingResource registerLocalResource(EmbeddingModel model, String apiUrl) {
        EmbeddingResource resource = new EmbeddingResource(model);
        resource.setApiUrl(apiUrl);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }

    /**
     * 注册本地Embedding模型资源
     * @param model
     * @param id
     * @param apiUrl
     * @return
     */
    public static EmbeddingResource registerLocalResource(EmbeddingModel model,String id, String apiUrl) {
        EmbeddingResource resource = new EmbeddingResource(model, id, null);
        resource.setApiUrl(apiUrl);
        EmbeddingResourceCenter.register(resource);
        return resource;
    }


    /**
     * 根据id，取消注册某个ai资源
     * @param id
     */
    public static void unregisterResource(String id) {
        EmbeddingResource resource = EmbeddingResourceCenter.getResourceById(id);
        if (resource != null) {
            EmbeddingResourceCenter.unregister(resource);
        }
    }

    /**
     * 对给定的文件进行向量化
     * @param resourceId
     * @param text
     * @return
     */
    public static EmbeddingEvent embedding(String resourceId, String... text) {
        return new EmbeddingEvent(resourceId, text);
    }


    /**
     * 对给定的文件进行向量化
     * @param resourceId
     * @param text
     * @return
     */
    public static EmbeddingEvent embedding(String resourceId, List<String> text) {
        return new EmbeddingEvent(resourceId, text);
    }
    /**
     * 对给定的文件进行向量化
     * @param model
     * @param text
     * @return
     */
    public static EmbeddingEvent embedding(EmbeddingModel model, String... text) {
        return new EmbeddingEvent(model, text);
    }


    /**
     * 对给定的文件进行向量化
     * @param model
     * @param text
     * @return
     */
    public static EmbeddingEvent embedding(EmbeddingModel model, List<String> text) {
        return new EmbeddingEvent(model, text);
    }

    /**
     * 取消某个事件的执行
     * @param eventId
     */
    public static void cancel(String eventId) {
        EventBus.cancel(eventId);
    }


}
